/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.cms.portlet.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import org.w3c.dom.Element;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;
import com.x4ee.soa.portal.framework.core.FrameworkConfig;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-8-7上午11:48:37
 */
public class URICreateActionFilter extends BaseRequestFilter implements
		ActionFilter {
	public static String getFileName(String fullname){
		String filename = fullname;
		if(filename.indexOf(File.separator)>-1){
			filename=filename.substring(filename.lastIndexOf(File.separator)+1,filename.length());
		}
		if(filename.indexOf("/")>-1){
			filename=filename.substring(filename.lastIndexOf("/")+1,filename.length());
		}
		if(filename.indexOf("\\")>-1){
			filename=filename.substring(filename.lastIndexOf("\\")+1,filename.length());
		}
		return filename;
	}
	private FrameworkConfigWraper tFrameworkConfigWraper =null;
	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {
		String uriName = actionRequest.getParameter("uriName");
		String jspName = actionRequest.getParameter("jspName");
		if((uriName!=null)&&(jspName!=null)){
			jspName = getFileName(jspName);
			tFrameworkConfigWraper.addUri(uriName,"jsp"+"/"+jspName);
			Map frameworkUriMap = new HashMap();
			Map uriMap = FrameworkConfigWraper.getURIMapByRenderName(tFrameworkConfigWraper.getXmlConfig(),"ViewIndex");
			for(Iterator rIt = uriMap.keySet().iterator();rIt.hasNext();){
				Object keyObj = rIt.next();
				if((keyObj!=null)&&(keyObj instanceof String)){
					String key = (String)keyObj;
					Object valueObj = uriMap.get(keyObj);			
					if((valueObj!=null)&&(valueObj instanceof String)){
						String mValue = (String)valueObj;
						if(mValue.indexOf("jsp"+"/")>-1){
							String tValue= mValue.substring(mValue.lastIndexOf("jsp"+"/")+4, mValue.length());
							frameworkUriMap.put(key, tValue);
						}
					}
				}
			}
			actionRequest.getPortletSession().setAttribute(FrameworkConfigWraper.FRAMEWORK_URI_MAP,frameworkUriMap);
			return FrameworkConfigWraper.DEFAULT;
		}else{
			return FrameworkConfigWraper.ERROR;
		}
	}

	public void init(Element tXmlConfig, FrameworkConfig frameworkConfig) throws PortletException {
		super.init(tXmlConfig, frameworkConfig);
		tFrameworkConfigWraper = new FrameworkConfigWraper(tXmlConfig,frameworkConfig);		
	}
}
